﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Hotel_Admin.Common;
using HOTEL.Dao;
using Hotel.Util;
using Hotel.Domain;
using System.Drawing;
using Hotel_Admin.Web.Common;

namespace Hotel_Admin
{
    public partial class KH_QuanLyPhong : Authentication
    {
        private PhieuDatPhongDao pdpDao = new PhieuDatPhongDao();
        private PhongDao phongDao = new PhongDao();
        private DVTDao dvtDao = new DVTDao();
        private DichVuDao dvDao = new DichVuDao();
        protected void Page_Load(object sender, EventArgs e)
        {

            CheckViewPermission();
            if (!Page.IsPostBack)
            {
                LoadReference();
                BindDataForGird();
            }
        }
        protected void LoadReference()
        {
            CommonFunc.BindDropDownList(ddlDayPhong, new DayPhongDao().GetList(), "TenDay", "MaDay", true);
            CommonFunc.BindDropDownList(ddlHangPhong, new HangPhongDao().GetList(), "TenHangPhong", "MaHangPhong", true);
            CommonFunc.BindDropDownList(ddlLoaiPhong, new LoaiPhongDao().GetList(), "TenLoaiPhong", "MaLoaiPhong", true);
            CommonFunc.BindDropDownList(ddlTang, new TangDao().GetList(), "TenTang", "MaTang", true);
           
        }
        protected bool IsValidData()
        {
            if (String.IsNullOrEmpty(txtTenPhong.Text.Trim()))
            {
                ShowError("Tên không được trống", txtTenPhong.ClientID);
                return false;
            }
            return true;
        }
        protected void BindDataForGird()
        {
            var maphong = ConvertUtil.ToInteger(txtMaPhong.Text.Trim());
            var tenphong =(String) txtTenPhong.Text.Trim();
            var maloaiphong = ConvertUtil.ToInteger(ddlLoaiPhong.SelectedValue);
            var matang = ConvertUtil.ToInteger(ddlTang.SelectedValue);
            var mahang = ConvertUtil.ToInteger(ddlHangPhong.SelectedValue);
            var maday = ConvertUtil.ToInteger(ddlDayPhong.SelectedValue);
            var list = pdpDao.GetListPhieuThuePhong(maphong, tenphong, mahang, maloaiphong, maday, matang, TRANGTHAIPHONG.DANGSUDUNG.GetHashCode());
            gvList.DataSource = list;
            gvList.DataBind();
            upGird.Update();
        }
        protected void ClearForm()
        {
            txtTenPhong.Text = "";
            txtMaPhong.Text = "";
            upInfor.Update();
        }
        protected void CheckViewPermission()
        {
            if (!HasViewPermission(Pages.KH_QuanLyPhong.GetHashCode()))
            {
                Redirect(WebURL.UrlWebDinie);
            }
        }

        protected void gvList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            var require = e.CommandName;
            var id = ConvertUtil.ToInteger(e.CommandArgument.ToString());
            if (!id.HasValue) return;
            var gvrow = (((LinkButton)e.CommandSource).NamingContainer);
            if (gvrow == null) return;
            var txtSoLuong = gvrow.FindControl("txtSoLuong") as TextBox;
            var ddlDichvu = gvrow.FindControl("ddlLoaiDichVu") as DropDownList;
            var ddlDonViTinh = gvrow.FindControl("ddlDonViTinh") as DropDownList;
            var phieuthue = pdpDao.GetPhieuThuePhong(id.Value);
            switch (require)
            {
                case "AddDichVu":
                    var soluong = ConvertUtil.ToInteger(txtSoLuong.Text.Trim());
                    if (!soluong.HasValue)
                    {
                        ShowError("Số lượng phải là số nguyên",txtSoLuong.ClientID);
                        CloseWaitingDialog();
                        return;
                    }
                    else
                    {
                        if (!HasInsertPermission(Pages.KH_QuanLyPhong.GetHashCode()))
                        {
                            Redirect(WebURL.UrlWebDinie);
                        }
                        var dichvu = dvDao.Get(int.Parse(ddlDichvu.SelectedValue));
                        if (dichvu != null)
                        {
                            var sudungdichvu = new SU_DUNG_DICH_VU
                            {
                                MaPhieuThue = phieuthue.MaPhieuThue,
                                MaDichVu = int.Parse(ddlDichvu.SelectedValue),
                                MaDonViTinh = int.Parse(ddlDonViTinh.SelectedValue),
                                SoLuong = soluong.Value,
                                DonGia=dichvu.Gia,
                                NgaySuDung=DateTime.Now,
                            };
                            var msg = pdpDao.ThemDichVu(sudungdichvu);
                            if (msg.MsgType != MessageType.Info)
                            {
                                ShowError("Thêm không thành công");
                                CloseWaitingDialog();
                                return;
                            }
                            else
                            {
                                ShowInfor("Thêm dịch vụ thành công");
                                CloseWaitingDialog();
                            }
                        }
                    }
                    break;
                case "EditItem":
                    if (phieuthue != null)
                    {
                        PhieuThueChosen = phieuthue;
                        BindChiTietDichVu();
                    }
                    break;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            
        }

        protected void btnHuy_Click(object sender, EventArgs e)
        {
            ClearForm();
            CloseWaitingDialog();
        }

        protected void btnFilter_Click(object sender, EventArgs e)
        {
            BindDataForGird();
            CloseWaitingDialog();
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
        }

        protected void gvList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (!e.Row.RowType.Equals(DataControlRowType.DataRow)) return;
            var list = gvList.DataSource as List<PHIEU_THUE_PHONG>;
            var panel = e.Row.FindControl("status") as Panel;
            if (panel != null)
            {
                var item = list[e.Row.RowIndex + gvList.PageIndex * gvList.PageSize];
                panel.BackColor = Color.FromName(item.PHONG.TRANG_THAI.Color);
            }
            var ddlDVT = e.Row.FindControl("ddlDonViTinh") as DropDownList;
            var ddlDichVu = e.Row.FindControl("ddlLoaiDichVu") as DropDownList;
            if (ddlDVT == null || ddlDichVu == null) return;
            CommonFunc.BindDropDownList(ddlDVT, dvtDao.GetList(), "TenDonViTinh", "MaDonViTinh", false);
            CommonFunc.BindDropDownList(ddlDichVu, dvDao.GetList(), "TenDV", "MaDichVu", false);
        }
        private void BindChiTietDichVu()
        {
            var listchitiet = pdpDao.GetSuDungDichVuListFromPhieuThue(PhieuThueChosen.MaPhieuThue);
            gvDetail.DataSource = listchitiet;
            gvDetail.DataBind();
            divChiTiet.Visible = listchitiet.Count > 0;
            upChitiet.Update();
        }
        protected PHIEU_THUE_PHONG PhieuThueChosen
        {
            get
            {
                return Session[SessionKey.SessionCurrernPhieuSuDungDichVu] as PHIEU_THUE_PHONG;
            }
            set
            {
                Session[SessionKey.SessionCurrernPhieuSuDungDichVu] = value;
            }
        }
        protected void gvDetail_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            var require = e.CommandName;
            var id = ConvertUtil.ToInteger(e.CommandArgument.ToString());
            if (!id.HasValue) return;
            var phieuthuedv = pdpDao.GetSuDungDichVu(id.Value);
            switch (require)
            {
                case "XoaDichVu":
                    if (HasDeletePermission(Pages.KH_QuanLyPhong.GetHashCode()))
                    {
                        if (phieuthuedv != null)
                        {
                            var msg = pdpDao.XoaDichVu(phieuthuedv);
                            if (msg.MsgType != MessageType.Info)
                            {
                                ShowError("Xóa không thành công");
                                CloseWaitingDialog();
                                return;
                            }
                            else
                            {
                                ShowError("Xóa dịch vụ thành công");
                                BindDataForGird();
                                BindChiTietDichVu();
                                CloseWaitingDialog();
                            }
                        }
                    }
                    else
                    {
                        Redirect(WebURL.UrlWebDinie);
                    }
                    break;
            }
        }

    }
}
